package com.hurl.waguji.web.admin;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.hibernate.SQLQuery;
import org.hibernate.ScrollableResults;
import org.hibernate.tool.hbm2x.StringUtils;

import com.hurl.library.util.DateUtils;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.base.dao.HibernateService;
import com.hurl.waguji.persistence.DownloadResultBO;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadLogAction extends ActionSupport {
	private int start;
	private int limit;
	
	private int totalProperty;
	private List<Map<String,Object>> root = new ArrayList<Map<String,Object>>();
	
	private String ids;
	private Integer result;
	
	public Integer getResult() {
		return result;
	}

	public void setResult(Integer result) {
		this.result = result;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public List<Map<String, Object>> getRoot() {
		return root;
	}

	public void setRoot(List<Map<String, Object>> root) {
		this.root = root;
	}

	public String execute() throws Exception {
		HibernateDao dao = HibernateService.getDao();
		totalProperty = ((BigInteger)dao.queryBySQL(SqlReader.getSql("SQL000001"))
								.list()
								.get(0)).intValue();
		SQLQuery query = dao.queryBySQL(SqlReader.getSql("SQL000002", new Object[]{start,limit}));
		ScrollableResults result = query.scroll();
		while(result.next()){
			Map<String,Object> map = new HashMap<String,Object>();
			if(result.get(0)!=null){
				map.put("rq", DateUtils.formatDate((Date)result.get(0), DateUtils.FORMAT_DATE_SIMPLE));
			}else{
				map.put("rq", "");
			}
			map.put("title", String.valueOf(result.get(1)));
			map.put("startTime", DateUtils.formatDate((Date)result.get(2), DateUtils.FORMAT_DATE_TIME_SIMPLE));
			map.put("useTime", DateUtils.periodInMinutes((Date)result.get(2), (Date)result.get(3)));
			map.put("count", (Integer)result.get(4));
			String success = String.valueOf(result.get(5));
			if(StringUtils.equals(success, "1")){
				map.put("result", "【成功】执行成功！");
			}else{
				map.put("result","【失败】"+ (String)result.get(6));
			}
			map.put("id", (Integer)result.get(7));
			root.add(map);
		}
		return SUCCESS;
	}
	public String delete()throws Exception{
		if(StringUtils.isNotEmpty(ids)){
			List<Long> idlist = (List<Long>)JSONUtil.deserialize(this.ids);
			if(!idlist.isEmpty()){
				HibernateDao dao = HibernateService.getDao();
				for(Long id : idlist){
					DownloadResultBO bo = new DownloadResultBO();
					bo.setId(id.intValue());
					dao.delete(bo);
				}
				dao.flush();
				dao.clear();
			}
		}
		result = 1;
		return SUCCESS;
	}
}
